package com.cirkana.viewer.data.game;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cirkana.data.IDataColumnDef;
import com.cirkana.data.IDataTableDef;
import com.cirkana.data.game.IGameEstate;
import com.cirkana.serial.bin.BinarySerializationException;
import com.cirkana.serial.bin.BinarySerializationUtil;
import com.cirkana.serial.text.TextSerializationBuilder;
import com.cirkana.viewer.data.DataColumnDef;
import com.cirkana.viewer.data.LocalDataRecord;

public class GameEstate extends LocalDataRecord implements IGameEstate {

	protected static final String COLUMN_ROUTE_ID = "ROUTE_ID";
	protected static final String COLUMN_USER_ID = "USER_ID";
	protected static final String COLUMN_START_DATE = "START_DATE";
	protected static final String COLUMN_STATUS = "STATUS";

	@Override
	public Integer getRouteId() {
		return getIntegerField(COLUMN_ROUTE_ID);
	}

	@Override
	public void setRouteId(Integer routeId) {
		setIntegerField(COLUMN_ROUTE_ID, routeId);
	}

	@Override
	public Integer getUserId() {
		return getIntegerField(COLUMN_USER_ID);
	}

	@Override
	public void setUserId(Integer userId) {
		setIntegerField(COLUMN_USER_ID, userId);
	}

	@Override
	public Date getStartDate() {
		return getDateField(COLUMN_START_DATE);
	}

	@Override
	public void setStartDate(Date startDate) {
		setDateField(COLUMN_START_DATE, startDate);
	}

	@Override
	public Integer getStatus() {
		return getIntegerField(COLUMN_STATUS);
	}

	@Override
	public void setStatus(Integer status) {
		setIntegerField(COLUMN_STATUS, status);
	}

	@Override
	public void serialize(DataOutputStream out) throws BinarySerializationException {
		BinarySerializationUtil.serializeInteger(getRouteId(), out);
		BinarySerializationUtil.serializeInteger(getUserId(), out);
		BinarySerializationUtil.serializeDate(getStartDate(), out);
		BinarySerializationUtil.serializeInteger(getStatus(), out);
	}

	@Override
	public void deserialize(DataInputStream in) throws BinarySerializationException {
		setRouteId(BinarySerializationUtil.deserializeInteger(in));
		setUserId(BinarySerializationUtil.deserializeInteger(in));
		setStartDate(BinarySerializationUtil.deserializeDate(in));
		setStatus(BinarySerializationUtil.deserializeInteger(in));
	}

	@Override
	public String toText() {
		TextSerializationBuilder t = new TextSerializationBuilder();
		t.append("routeId", getRouteId());
		t.append("userId", getUserId());
		t.append("startDate", getStartDate());
		t.append("status", getStatus());
		return t.getTextSerialization();
	}

	static class RouteTableDef implements IDataTableDef {

		private static final String TABLE_NAME = "ROUTE_ESTATES";
		private static final List<IDataColumnDef> TABLE_COLUMNS = new ArrayList<IDataColumnDef>();
		static {
			TABLE_COLUMNS.add(new DataColumnDef(0, COLUMN_ROUTE_ID, int.class, true, true));
			TABLE_COLUMNS.add(new DataColumnDef(1, COLUMN_USER_ID, Integer.class));
			TABLE_COLUMNS.add(new DataColumnDef(2, COLUMN_START_DATE, Date.class));
		}

		@Override
		public String getTableName() {
			return TABLE_NAME;
		}

		@Override
		public List<IDataColumnDef> getColumnsDef() {
			return TABLE_COLUMNS;
		}

		@Override
		public Class<?> getDataRecordType() {
			return GameEstate.class;
		}

	}

}
